<?php session_start();
require('../globals/setup.php');
$smarty = new Smarty_Kalender;	

$host  = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri= URI_L;}else{$uri= URI_P;}

if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
	if (!is_object($bd)){	
		//me conecto a la base de datos
		require("../globals/Db.class.php");
		require("../globals/Conf.class.php");
		$bd=Db::getInstance();	
	}
	if (!is_object($util)){	
		include("../globals/Util.class.php");		
		$util=Util::getInstance();
	}
	
	//$util->debugOn();
	

	if (!($util->administrarTiempoSesion())){
		echo "<script language=Javascript> location.href='http://$host$uri/timeout.php'; </script>";
	}else if (!($util->esCliente())){
		echo "<script language=Javascript> location.href='http://$host$uri/sinAcceso.php'; </script>";
	}
	
	
	function is_valid_email($email){
	  $result = true;
	  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
	    $result = false;
	  }
	  return $result;
	}
	
	$id_user = $_SESSION['id_user'];
	
	$titleHTML = "Kalender - Mi perfil";	
	
	$query_clientes = "select id, tipo, usuario, password, localidad, codigo_postal, fax, pais, nivel, "
	."razon_social, cuit, email, observaciones, contacto, direccion, telefono, logo from usuarios where id = $id_user;";
	$resultado = $bd->eje($query_clientes);		
	$num_rows = $bd->numRows($resultado);
	if ($num_rows == 1){
		$row = $bd->fila($resultado);
		$smarty->assign("id_update", $row["id"]);
		$smarty->assign("razon_social", $row["razon_social"]);
		$smarty->assign("direccion", $row["direccion"]);
		$smarty->assign("cuit", $row["cuit"]);
		$smarty->assign("tipo", $row["tipo"]);
		$smarty->assign("pais", $row["pais"]);
		$smarty->assign("contacto", $row["contacto"]);
		$smarty->assign("localidad", $row["localidad"]);
		$smarty->assign("codigo_postal", $row["codigo_postal"]);
		$smarty->assign("fax", $row["fax"]);
		$smarty->assign("email", $row["email"]);
		$smarty->assign("telefono", $row["telefono"]);
		$smarty->assign("logo", $row["logo"]);
		$smarty->assign("nivelUser", $row["nivel"]);
		$smarty->assign("observaciones", $row["observaciones"]);
		$smarty->assign("usuario", $row["usuario"]);
		$smarty->assign("password", $row["password"]);
		
		$pais = $row["pais"];
	}	
	
	$paises_options = array();	
	$result_countries = $bd->eje("SELECT id,name from countries");
	while ($row1 = $bd->fila($result_countries)){										
		$id = $row1["id"];
		$name = $row1["name"];		
		if ($pais == $name){
			array_push($paises_options, "<option value='".$name."' selected>".$name."</option>");
		}else{
			array_push($paises_options, "<option value='".$name."'>".$name."</option>");
		}
	}
	$smarty->assign("paises_options", $paises_options);
	$smarty->assign("paises_options_cant", sizeof($paises_options));
	
	

	$menuItem = "perfil";
	$smarty->assign("menuItem", $menuItem);
	include("../globals/menuIzquierdaClientes.php");
	
	$smarty->display("clientes/miperfil.tpl");

} ?>